package com.learn.firstBatis.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.firstBatis.dao.UserDao;
import com.learn.firstBatis.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.Collections;
import java.util.List;

public class UserService {
    /**
     * 根据id查询User对象
     * @param id
     * @return
     */
    public User findUser(Long id){
        SqlSession sqlSession = null;
        try {
            // 1.获取mybatis配置文件
            Reader resourceAsReader = Resources.getResourceAsReader("MybatisConfig.xml");
            // 2. 构建sqlSession工程
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsReader);
            // 3. 生产sqlSession
            sqlSession = build.openSession();
            // 4. 获取UserDao
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            User user = userDao.findUser(id);
            return user;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }
        return null;
    }

    /**
     * 新增创建保存User的方法
     * @param user
     */
    public void save(User user){
        SqlSession sqlSession = null;
        try {
            // 1.获取mybatis配置文件
            Reader resourceAsReader = Resources.getResourceAsReader("MybatisConfig.xml");
            // 2. 构建sqlSession工程
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsReader);
            // 3. 生产sqlSession, true表示进行提交commit
            sqlSession = build.openSession(true);
            // 4. 获取UserDao
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            userDao.save(user);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }
    }

    /**
     * 更新方法的具体实现
     * @param user
     * @return
     */
    public int update(User user){
        SqlSession sqlSession = null;
        try {
            // 1.获取mybatis配置文件
            Reader resourceAsReader = Resources.getResourceAsReader("MybatisConfig.xml");
            // 2. 构建sqlSession工程
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsReader);
            // 3. 生产sqlSession, true表示进行提交commit
            sqlSession = build.openSession();
            // 4. 获取UserDao
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            int update = userDao.update(user);
            // 手动提交commit，为了测试事务，可以在中间添加错误
            // 中间有错误时，数据会进行回退,不能进行修改数据库的数据
            // 测试事务回滚时添加下边这行报错
            // int err = 10/ 0;
            // 手动进行commit提交
            sqlSession.commit();
            return update;
        } catch (IOException e) {
            e.printStackTrace();
            //如果出错，进行数据回滚
            sqlSession.rollback(true);
        } finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }
        return  -1;
    }


    /**
     * 根据id删除 User
     * @param id
     * @return
     */
    public int deleteUserById(Long id){
        SqlSession sqlSession = null;
        try {
            // 1.获取mybatis配置文件
            Reader resourceAsReader = Resources.getResourceAsReader("MybatisConfig.xml");
            // 2. 构建sqlSession工程
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsReader);
            // 3. 生产sqlSession, true表示进行提交commit
            sqlSession = build.openSession(true);
            // 4. 获取UserDao
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            return userDao.deleteById(id);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }
        return  -1;
    }

    /**
     * 查询出大于指定age的user列表
     * @param age
     * @return
     */
    public List<User> findUserListGtAge(int age){
        SqlSession sqlSession = null;
        try {
            // 1.获取mybatis配置文件
            Reader resourceAsReader = Resources.getResourceAsReader("MybatisConfig.xml");
            // 2. 构建sqlSession工程
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsReader);
            // 3. 生产sqlSession, true表示进行提交commit
            sqlSession = build.openSession(true);
            // 4. 获取UserDao
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            List<User> userListGtAge = userDao.findUserListGtAge(age);
            return userListGtAge;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }
        return  Collections.emptyList();
    }

/**
 * 分页查询yser列表
 * @param page
 * @param pageSize
 * @return
 */
public List<User> findAllUserPage(int page, int pageSize){
    SqlSession sqlSession = null;
    try {
        // 1.获取mybatis配置文件
        Reader resourceAsReader = Resources.getResourceAsReader("MybatisConfig.xml");
        // 2. 构建sqlSession工程
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsReader);
        // 3. 生产sqlSession, true表示进行提交commit
        sqlSession = build.openSession(true);
        // 4. 获取UserDao
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        long count = userDao.countFindAllUser();
        System.out.println("all count: "+ count);

        List<User> allUser = userDao.findAllUser((page - 1) * pageSize, pageSize);
        return  allUser;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(sqlSession != null){
            sqlSession.close();
        }
    }
    return  Collections.emptyList();
}


    public List<User> findAllUserPagePlugin(int page, int pageSize){
        SqlSession sqlSession = null;
        try {
            // 1.获取mybatis配置文件
            Reader resourceAsReader = Resources.getResourceAsReader("MybatisConfig.xml");
            // 2. 构建sqlSession工程
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsReader);
            // 3. 生产sqlSession, true表示进行提交commit
            sqlSession = build.openSession(true);
            // 4. 获取UserDao
            UserDao userDao = sqlSession.getMapper(UserDao.class);

            PageHelper.startPage(page, pageSize);
            Page<User> allUserPage = userDao.findAllUserPagePlugin();
            System.out.println("all count: "+ allUserPage.getTotal());
            System.out.println("all page: " + allUserPage.getPages());

            return  allUserPage;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }
        return  Collections.emptyList();
    }

    public List<User> findUserListByCondition(int age){
        SqlSession sqlSession = null;
        try {
            // 1.获取mybatis配置文件
            Reader resourceAsReader = Resources.getResourceAsReader("MybatisConfig.xml");
            // 2. 构建sqlSession工程
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsReader);
            // 3. 生产sqlSession, true表示进行提交commit
            sqlSession = build.openSession(true);
            // 4. 获取UserDao
            UserDao userDao = sqlSession.getMapper(UserDao.class);

            List<User> userListByCondition = userDao.findUserListByCondition(age);
            return  userListByCondition;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }
        return  Collections.emptyList();
    }

    public List<User> findUserListByIds(List<Long> idList){
        SqlSession sqlSession = null;
        try {
            // 1.获取mybatis配置文件
            Reader resourceAsReader = Resources.getResourceAsReader("MybatisConfig.xml");
            // 2. 构建sqlSession工程
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsReader);
            // 3. 生产sqlSession, true表示进行提交commit
            sqlSession = build.openSession(true);
            // 4. 获取UserDao
            UserDao userDao = sqlSession.getMapper(UserDao.class);

            List<User> userListByIds = userDao.findUserListByIds(idList);
            return  userListByIds;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }
        return  Collections.emptyList();
    }
}
